package com.example.qiniu.controller;

import com.qiniu.util.Auth;
import com.qiniu.util.StringMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@CrossOrigin
@RestController
@RequestMapping("/qiniu")
public class QiniuController {
    /**
     * accessKey 七牛云key
     * secretKey 七牛云密码
     * bucket 云空间地址
     * */
    @RequestMapping("/gettoken")
    public Map<String, String> getQiniuUploadTokne(){
        String accessKey ="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        String secretKey ="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        String bucket ="XXXXXXXXX";
        Auth auth=Auth.create(accessKey,secretKey);
        //发送请求获得token
        String token=auth.uploadToken(bucket,null,86400,new StringMap().put("insertOnly", 0));
        //时间戳
        long time = System.currentTimeMillis();
        String nowTimeStamp = String.valueOf(time / 1000);
        Map<String,String> resmap=new HashMap<>();
        resmap.put("createtime",nowTimeStamp);
        resmap.put("token",token);
        return resmap;
    }
}
